<template>
	<view class="content">
		<view class="top_content">
			<up-search placeholder="请输入关键字" bg-color="white" v-model="data.keyword" @change="handleSearch"></up-search>
		</view>
		<view class="content_swiper">
			<up-swiper v-if="data.list.length" :list="data.list" keyName="image" radius="8" showTitle :autoplay="true"
				height="160" circular></up-swiper>
		</view>
		<view class="content_notice">
			<up-notice-bar bg-color="white" :text="data.text"></up-notice-bar>
		</view>
		<up-line color="#d8d8d8"></up-line>
		<view class="content_waterfail">
			<up-waterfall v-model="data.HomeList">
				<template v-slot:left="{leftList}">
					<view v-for="(item, index) in leftList" :key="index" class="demo_water" @click="goDetail(item)">
						<!-- 这里编写您的内容，item为您传递给v-model的数组元素 -->
						<up-lazy-load threshold="-450" border-radius="10" :image="item.img"
							:index="index"></up-lazy-load>
						<view class="demo_title">
							{{item.title}}
						</view>
						<view class="demo_date">
							{{item.times}}
						</view>
						<view class="demo_tag">
							<view v-for="(tags,index) in item.tag" :key="index">
								<view class="demo_tag_son">
									{{tags}}
								</view>
							</view>
						</view>
						<view class="demo_isDot" v-if="item.dot">
							{{item.isDot}}
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view v-for="(item, index) in rightList" :key="index" class="demo_water" @click="goDetail(item)">
						<!-- 这里编写您的内容，item为您传递给v-model的数组元素 -->
						<up-lazy-load threshold="-450" border-radius="10" :image="item.img"
							:index="index"></up-lazy-load>
						<view class="demo_title">
							{{item.title}}
						</view>
						<view class="demo_date">
							{{item.times}}
						</view>
						<view class="demo_tag">
							<view v-for="(tags,index) in item.tag" :key="index">
								<view class="demo_tag_son">
									{{tags}}
								</view>
							</view>
						</view>
						<view class="demo_isDot" v-if="item.dot">
							{{item.isDot}}
						</view>
					</view>
				</template>
			</up-waterfall>
		</view>
		<view class="topClass" v-if="data.showTop">
			<!-- 点击回到顶部 -->
			<up-icon name="arrow-upward" color="#ffffff" size="28" @click="handleBackTop"></up-icon>
		</view>
	</view>
</template>

<script setup>
	// 顶部导入
	import {
		getBanner,
		getHomeList
	} from '../../api/api.js'
	import {
		onLoad,
		onReachBottom,
		onPageScroll
	} from '@dcloudio/uni-app'
	import {
		reactive
	} from 'vue'
	// ----------------------------------
	const data = reactive({
		keyword: '',
		list: [],
		text: "项目数据只是模拟数据，并非真实数据，请注意甄别！",
		HomeList: [],
		pageSize: 10,
		pageNum: 1,
		showTop: false
	})
	const handleSearch = () => {
		console.log(data.keyword);
	}
	// 页面一加载就触发
	onLoad(() => {
		getBanner().then(res => {
			data.list = res.bannerList
		})

		getHomeList().then(res => {
			data.HomeList = res
		})
	})
	// 点击详情触发
	const goDetail = (item) => {
		const en = JSON.stringify(item)
		uni.navigateTo({
			url:`/pages/detail/detail?item=${encodeURIComponent(en)}`
		})
	}
	// 页面触底之后触发
	onReachBottom(() => {
		console.log("页面触底了·····");
		// 真实后端接口请求
		// getBanner({
		// 	pageSize: data.pageSize,
		// 	pageNum: data.pageNum++
		// }).then(res => {
		// 	data.HomeList = [...data.HomeList, ...res]
		// })

		// 模拟后端数据
		setTimeout(() => {
			addTimeOutList()
		}, 1000)
	})

	const addTimeOutList = () => {
		for (let index = 0; index < 10; index++) {
			// 接受两个参数，一个最小值，一个最大值
			let i = uni.$u.random(0, data.HomeList.length - 1)
			let item = JSON.parse(JSON.stringify(data.HomeList[i]))
			item.id = uni.$u.guid()
			data.HomeList.push(item)
		}
	}

	// 回到顶部什么时候显示
	onPageScroll((e) => {
		if (e.scrollTop > 600) {
			data.showTop = true
		} else {
			data.showTop = false
		}
	})

	// 点击回到顶部
	const handleBackTop = () => {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 300
		})
	}
</script>
<style>
	page {
		background-color: #f4f4f4;
	}
</style>

<style lang="scss" scoped>
	.content {
		padding: 20rpx;

		.top_content {
			margin-bottom: 10rpx;
		}

		.content_swiper {
			margin-bottom: 10rpx;
		}

		.content_notice {
			margin-bottom: 10rpx;
		}

		.topClass {
			border-radius: 50%;
			padding: 8rpx;
			opacity: 0.8;
			background-color: #acacac;
			color: white;
			position: fixed;
			bottom: 20rpx;
			right: 20rpx;
		}

		.content_waterfail {
			margin: 20rpx;
		}

		.demo_water {
			position: relative;
			margin: 10rpx 10rpx 10rpx 0;
			background-color: white;
			border-radius: 16px;
			padding: 16rpx;

			.demo_title {
				font-size: 30rpx;
				color: black;
				margin-top: 10rpx;
			}

			.demo_date {
				font-size: 22rpx;
				color: #777;
				margin-top: 10rpx;
			}

			.demo_tag {
				display: flex;
				margin-top: 10rpx;

				.demo_tag_son {
					padding: 5rpx;
					margin-right: 10rpx;
					font-size: 16rpx;
					color: #777;
					border-radius: 20%;
					border: #777 2rpx solid;
				}
			}

			.demo_isDot {
				width: 50rpx;
				position: absolute;
				top: 15rpx;
				right: 15rpx;
				line-height: 20rpx;
				padding: 8rpx;
				border-radius: 10rpx;
				text-align: center;
				background-color: red;
				font-size: 20rpx;
				color: white;
			}
		}
	}
</style>